<{include file="public/header" /}>

<?php

    use Tools\AdminPermission;

    $role_id = Session::get('role_id');
    $adminPermission = new AdminPermission();
    $adminPermission->setRoleId($role_id);
    $module_name = 'admin';
?>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <textarea id="list_filters_box"style="display: none"></textarea>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">搜索内容</label>
                            <div class="layui-input-inline" style="min-width: 400px">
                                <input type="text" name="filters[search_text]" autocomplete="off" class="layui-input" placeholder="姓名/用户名/昵称/手机号">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <{if checkAdminPermissionByAction($adminPermission,$module_name,'create')}>
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <{/if}>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail">详情</a>

            <{if checkAdminPermissionByAction($adminPermission,$module_name,'update')}>
                {{#  if(d.role_id == 1){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-disabled" onclick="error_msg('不可以编辑超级管理员权限')">编辑</a>
                {{#  } else { }}
                    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="update">编辑</a>
                {{#  } }}
            <{/if}>

            <{if checkAdminPermissionByAction($adminPermission,$module_name,'delete')}>
                {{#  if(d.role_id == 1){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-disabled" onclick="error_msg('不可以删除超级管理员权限')">删除</a>
                {{#  } else { }}
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
                {{#  } }}
            <{/if}>

        </script>
        <script type="text/html" id="userNameTextTpl">
            {{d.user.user_name}}
        </script>
        <script type="text/html" id="trueNameTextTpl">
            {{d.user.true_name}}
        </script>
        <script type="text/html" id="nickNameTextTpl">
            {{d.user.nick_name}}
        </script>
        <script type="text/html" id="roleNameTextTpl">
            {{d.role.title}}
        </script>
        <script type="text/html" id="phoneTextTpl">
            {{d.user.phone}}
        </script>
        <script type="text/html" id="certificationStatusTpl">
            {{#  if(d.user.certification_status == 100){ }}
            已认证
            {{#  }else if(d.user.certification_status == 1){ }}
            待审核
            {{#  }else if(d.user.certification_status == 3){ }}
            审核驳回
            {{#  } else { }}
            待认证
            {{#  } }}
        </script>
        <script type="text/html" id="statusTpl">
            {{#  if(d.user.status == 1){ }}
            正常
            {{#  } else { }}
            禁用
            {{#  } }}
        </script>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var formSelects = layui.formSelects;

        table.render({
            elem: '#currentTableId',
            url: "<{:admin_api_url('admin/list')}>"
            <{include file="public/table_common" /}>,
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'user_name', title: '用户名', templet:"#userNameTextTpl"},
                {field: 'true_name', title: '姓名', templet:"#trueNameTextTpl"},
                {field: 'nick_name', title: '昵称', templet:"#nickNameTextTpl"},
                {field: 'role_name', title: '角色名称', templet:"#roleNameTextTpl"},
                {field: 'phone', title: '手机号', templet:"#phoneTextTpl"},
                {field: 'created_at', width: 180, title: '注册时间', sort: true},
                {field: 'certification_status', width: 110, title: '实名状态', templet:"#certificationStatusTpl", sort: true},
                {field: 'status', width: 110, title: '账户状态', templet:"#statusTpl", sort: true},
                {field: 'tool',title: '操作', minWidth: 200, fixed:'right',templet:'#currentTableBar', align: "center"}
            ]]
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            set_list_filters(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let url = "<{:admin_api_url('admin/create')}>";
                $.post(url,{'_method':'get'},function(res){
                    ajax_response_filtr(res,$adminConfig);
                    if(res.status == 200){
                        var index = layer.open({
                            title: '添加管理员',
                            type: 1,
                            shade: 0.2,
                            shadeClose: true,
                            area: ['480px', '320px'],
                            btn: ['确定', '取消'],
                            content: res.data,
                            success: function(layero, index){

                                formSelects.config('user-id-select', {
                                    type: 'post',
                                    response: {
                                        statusCode: 200,            //成功状态码
                                        statusName: 'status',       //code key
                                        msgName: 'message',         //msg key
                                        dataName: 'data'            //data key
                                    },
                                    searchUrl: "<{:admin_api_url('user/search',['is_admin'=>2])}>",
                                    searchName: 'search_text',
                                    keyName: 'name',
                                    keyVal: 'id',
                                    delay: 500
                                });

                                form.render();
                            },
                            yes: function(index, layero){

                                let submitID = 'LAY-admin-add-submit';
                                let submit = layero.find('#'+ submitID);

                                //监听提交
                                form.on('submit('+ submitID +')', function(data){
                                    var field = data.field; //获取提交的字段
                                    if((!field.user_id) || field.user_id < 1){
                                        error_msg('请选择关联用户！');
                                        return false;
                                    }

                                    //提交
                                    $.post(url,field,function(res){
                                        ajax_response_filtr(res,$adminConfig);
                                        if(res.status == 200){
                                            layer.close(index); //关闭弹层

                                            //执行搜索重载
                                            table.reload('currentTableId', {
                                                page: {curr: 1},
                                                where:{filters:get_list_filters()}
                                            }, 'data');

                                            success_msg('添加成功');
                                        }

                                    },'json')

                                });

                                submit.trigger('click');
                            },
                        });
                    }
                },'json');

                return false;

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            //console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {

                let url = "<{:admin_api_url('admin/detail')}>";
                $.post(url,{'_method':'get', id: data.id},function(res){
                    ajax_response_filtr(res,$adminConfig);
                    if(res.status == 200){

                        var index = layer.open({
                            title: '用户详情',
                            type: 1,
                            shade: 0.2,
                            shadeClose: true,
                            area: ['90%', '90%'],
                            content: res.data,
                        });

                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                    }
                },'json');

                return false;
            }else if (obj.event === 'update') {

                let url = "<{:admin_api_url('admin/update')}>";
                $.post(url,{'_method':'get', id: data.id},function(res){
                    ajax_response_filtr(res,$adminConfig);
                    if(res.status == 200){

                        var index = layer.open({
                            title: '更新信息',
                            type: 1,
                            shade: 0.2,
                            shadeClose: true,
                            area: ['480px', '320px'],
                            btn: ['确定', '取消'],
                            content: res.data,
                            success: function (layero, index) {
                                form.render();
                            },
                            yes: function (index, layero) {

                                let submitID = 'LAY-admin-edit-submit';
                                let submit = layero.find('#' + submitID);

                                //监听提交
                                form.on('submit(' + submitID + ')', function (data) {
                                    var field = data.field; //获取提交的字段
                                    //提交
                                    $.post(url, field, function (res) {
                                        ajax_response_filtr(res,$adminConfig);
                                        if (res.status == 200) {
                                            layer.close(index); //关闭弹层

                                            //执行搜索重载
                                            table.reload('currentTableId', {
                                                page: {curr: 1},
                                                where: {filters: get_list_filters()}
                                            }, 'data');

                                            success_msg('添加成功');
                                        }

                                    }, 'json')

                                });

                                submit.trigger('click');
                            }
                        });
                    }
                },'json');

                return false;
            } else if (obj.event === 'delete') {

                let status = data.status;
                layer.confirm('您真的要删除Ta的管理员权限吗？', function (index) {
                    layer.close(index);
                    let url = "<{:admin_api_url('admin/delete')}>";
                    $.post(url,{id: data.id},function(res){
                        ajax_response_filtr(res,$adminConfig);
                        if(res.status == 200){
                            obj.del();
                            success_msg('操作成功');
                        }
                    },'json');
                });
            }
        });

    });
</script>

<{include file="public/footer" /}>